package cn.lds.healthj377.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Objects;

public class LoginInterceptor implements HandlerInterceptor {


  @Override
  public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
    throws Exception {
    HttpSession session = request.getSession();

    Object loginUser = session.getAttribute("loginUser");

    if (Objects.isNull(loginUser)){
      request.setAttribute("loginErr","需要登陆后访问");
      request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request,response);
    }
    return true;
  }

}